const path = require("path");

// 打印初始环境信息
console.log("Initial NODE_ENV:", process.env.NODE_ENV);

// 获取环境变量文件路径
const envPath = path.join(
  __dirname,
  "..",
  process.env.NODE_ENV === "production" ? ".env.production" : ".env.development"
);

console.log("Loading env from:", envPath);

// 加载环境变量
require("dotenv").config({ path: envPath });

// 设置默认值，防止 undefined
const config = {
  database: {
    host: process.env.DB_HOST || "localhost",
    port: parseInt(process.env.DB_PORT || "3306"),
    user: process.env.DB_USER || "koa_user",
    password: process.env.DB_PASSWORD || "Wang115158..",
    database: process.env.DB_DATABASE || "koa_db",
  },
};

// 打印详细的配置信息
console.log("==== Database Configuration ====");
console.log("Environment Variables:");
console.log("DB_HOST:", process.env.DB_HOST);
console.log("DB_PORT:", process.env.DB_PORT);
console.log("DB_USER:", process.env.DB_USER);
console.log("DB_DATABASE:", process.env.DB_DATABASE);

console.log("\nFinal Config:");
console.log("host:", config.database.host);
console.log("port:", config.database.port);
console.log("user:", config.database.user);
console.log("database:", config.database.database);
console.log("==============================");

module.exports = config;
